package com.oglvip.business.web.controller;

import com.google.common.collect.Maps;
import com.oglvip.business.consts.common.ResponseCode;
import com.oglvip.business.dto.ApplyInfoFormDto;
import com.oglvip.business.dto.DeliveryFormDto;
import com.oglvip.business.dto.PageBean;
import com.oglvip.business.dto.ResponseData;
import com.oglvip.business.remote.service.api.OrderService;
import com.oglvip.business.remote.service.api.StoreService;
import com.oglvip.business.remote.service.entity.*;
import com.oglvip.business.util.EncryptUtil;
import com.oglvip.business.util.SignUtil;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.TreeMap;

/**
 * @auther hanchao
 * @date 2016/12/24 1:27
 */
@RestController
@RequestMapping("/store")
public class StoreController {
    @Autowired
    private StoreService storeService;
    @Autowired
    private OrderService orderService;

    @RequestMapping("/banks")
    public ResponseData getBanks(){
        return storeService.getBanks();
    }

    @RequestMapping("/setBankinfo")
    public ResponseData setBankinfo(String bank,String bankNo,String bankMaster){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        StoreAccount account = (StoreAccount) authentication.getPrincipal();

        Long timestamp = System.currentTimeMillis();
        TreeMap<String,Object> map = Maps.newTreeMap();
        map.put("ticket",account.getTicket());
        map.put("bank",bank);
        map.put("bankNo",bankNo);
        map.put("bankMaster",bankMaster);
        map.put("timestamp",timestamp);
        String sig = SignUtil.getSign(map);
        return storeService.setBankInfo(account.getTicket(),bank,bankNo,bankMaster,timestamp,sig);
    }
    @RequestMapping("/getBankinfo")
    public ResponseData getBankinfo(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        StoreAccount account = (StoreAccount) authentication.getPrincipal();
        Long timestamp = System.currentTimeMillis();
        TreeMap<String,Object> map = Maps.newTreeMap();
        map.put("ticket",account.getTicket());
        map.put("timestamp",timestamp);
        String sig = SignUtil.getSign(map);
        return storeService.getBankInfo(account.getTicket(),timestamp,sig);
    }
    @RequestMapping("applyCash")
    public ResponseData applyCash(Integer money,String password){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        StoreAccount account = (StoreAccount) authentication.getPrincipal();
        String pass = EncryptUtil.md5Encrypt(password);
        Long timestamp = System.currentTimeMillis();
        TreeMap<String,Object> map = Maps.newTreeMap();
        map.put("ticket",account.getTicket());
        map.put("money",money);
        map.put("timestamp",timestamp);
        map.put("password",pass);
        String sig = SignUtil.getSign(map);
        return storeService.applyCash(account.getTicket(),money,pass,timestamp,sig);
    }

    @RequestMapping("/detail")
    public ResponseData<StoreDetail> getDetail() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        StoreAccount account = (StoreAccount) authentication.getPrincipal();
        return storeService.getDetail(account.getStoreId());
    }


    @RequestMapping("/setDetail")
    public ResponseData setDetail(ApplyInfoFormDto dto){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        StoreAccount account = (StoreAccount) authentication.getPrincipal();
        dto.setTicket(account.getTicket());
        dto.setStoreId(account.getStoreId());
        return storeService.modify(dto);
    }

    @RequestMapping("/overview")
    public ResponseData overview(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        StoreAccount account = (StoreAccount) authentication.getPrincipal();
        return orderService.overview(account.getTicket(),account.getStoreId());
    }

    @RequestMapping("/delivery")
    public ResponseData getDelivery(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        StoreAccount account = (StoreAccount) authentication.getPrincipal();
        ResponseData<Delivery> responseData = storeService.getDelivery(account.getTicket());
        responseData.get().transferDelivery();
        return responseData;
    }

    @RequestMapping("/setDelivery")
    public ResponseData setDelivery(DeliveryFormDto dto){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        StoreAccount account = (StoreAccount) authentication.getPrincipal();
        dto.setTicket(account.getTicket());
        return storeService.setDelivery(dto.transferDto());
    }


    /**
     *
     * @param pageBean
     * @param balanceNo
     * @param startTime
     * @param endTime
     * @return
     */
    @RequestMapping("/balanceList")
    public ResponseData pageList(PageBean pageBean, String balanceNo, String startTime,String endTime) throws ParseException {

        if(startTime != null){
            startTime = DateFormatUtils.ISO_DATE_FORMAT.parse(startTime).getTime() + "";
        }
        if(endTime != null){
            endTime = (DateFormatUtils.ISO_DATE_FORMAT.parse(endTime).getTime() + (24*60*60*1000-1)) + "";
        }
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        StoreAccount account = (StoreAccount) authentication.getPrincipal();
        ResponseData<PageData> resp = storeService.listBalance(account.getTicket(),account.getStoreId(),balanceNo,startTime,endTime,pageBean.getFirstResult(),pageBean.getOnePageSize());
        PageData pageData = resp.get();
        ResponseData result = new ResponseData();
        result.setResponseCode(ResponseCode.SUCCESS);
        pageBean.setTotalResults(pageData.getSize());
        pageBean.setData(pageData.getList());
        result.setData(pageBean);
        return result;
    }
}
